//
//  MyQrcodeViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/8.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MyQrcodeViewController.h"
#import <CoreImage/CoreImageDefines.h>

@interface MyQrcodeViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *myQRCodeView;

@end

@implementation MyQrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //还原滤镜默认属性
    [filter setDefaults];
    
    //将要生成二维码的数据转换为二进制
    NSString *str = @"辣条地址给我呀，还说过七夕！！";
    
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    
    //将生成好的二进制数据传给滤镜，让他生成数据，并输出图片
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    CIImage *outputImage = [filter outputImage];
    
    //显示图片
    UIImage *QRCodeImage = [UIImage imageWithCIImage:outputImage];
    
    UIImage *iconImage = [UIImage imageNamed:@"1"];
    
//    self.myQRCodeView.image = QRCodeImage;
    
    self.myQRCodeView.image = [self creatQRCodeWithIcon:iconImage QRCode:QRCodeImage];
    
}

#pragma mark ----创建带头像的二维码图片

-(UIImage *)creatQRCodeWithIcon:(UIImage *) icon QRCode:(UIImage *)QrcodeImage{
    
    
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(QrcodeImage.size, YES, 0.0);
    
    //绘制背景
    [QrcodeImage drawInRect:CGRectMake(0, 0, QrcodeImage.size.width, QrcodeImage.size.height)];
    //绘制头像
    [icon drawInRect:CGRectMake((QrcodeImage.size.width-10)/2, (QrcodeImage.size.height-10)/2, 10  , 10)];
    
    //取出图片
    UIImage *getImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    //返回图片
    return getImage;
    
}



@end
